<template>
    <div class="layout-padding">
        <div class="layout-padding-auto layout-padding-view">
            <!--查询表单-->
            <el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">

                <el-form-item :label="$t('basEquipment.equipmentCode')" prop="eqpCode">
                    <el-input v-model="state.queryForm.eqpCode" clearable :placeholder="$t('basEquipment.enterEquipmentCode')" />
                </el-form-item>

                <el-form-item :label="$t('basEquipment.equipmentName')" prop="eqpName">
                    <el-input v-model="state.queryForm.eqpName" clearable :placeholder="$t('basEquipment.enterEquipmentName')" />
                </el-form-item>

                <el-form-item :label="$t('basEquipment.equipmentSpecification')" prop="eqpSpec">
                    <el-input v-model="state.queryForm.eqpSpec" clearable :placeholder="$t('basEquipment.enterEquipmentSpecification')" />
                </el-form-item>

                <el-form-item :label="$t('basEquipment.equipmentType')" prop="eqpType">
                            <el-select v-model="state.queryForm.eqpType" style="width: 160px">
                                <el-option v-for="item in options" :key="item.value" :label="item.text" :value="item.value" />
                            </el-select>
                        </el-form-item>
                <el-form-item>
                    <el-button icon="el-icon-search" type="primary"  @click="getDataList">{{$t('basEquipment.query')}}</el-button>
                    <el-button icon="refresh"  @click="resetQuery">{{$t('basEquipment.reset')}}</el-button>
                    <el-button icon="el-icon-plus"  type="primary" @click="equiptmentDialogRef.openAddDialog()">{{$t('basEquipment.add')}}</el-button>
                    <el-button icon="el-icon-delete" type="danger" @click="handleDelete(selectObjs)">{{$t('basEquipment.delete')}}</el-button>
                </el-form-item>
            </el-form>
             <!--查询结果的列表-->
          <el-table :data="state.dataList" highlight-current-row
                    v-loading="state.loading" border style="width: 100%;height: 100%"
                    :cell-style="tableStyle.cellStyle" stripe :header-cell-style="tableStyle.headerCellStyle"
                    @row-click="clickRow"
                    @selection-change="selectionChangeHandle">
            <el-table-column type="selection" width="40" align="center"/>
            <el-table-column type="index" label="#" width="50" />
            <el-table-column prop="eqpCode" :label="$t('basEquipment.equipmentCode')"/>
            <el-table-column prop="eqpName" :label="$t('basEquipment.equipmentName')"/>

            <el-table-column prop="eqpType" :label="$t('basEquipment.equipmentType')" >
                <template #default="scope">
            <el-tag :type="scope.row.eqpType === 'eqp'?$t('basEquipment.equipment'):$t('basEquipment.cutter') ">
              {{ scope.row.eqpType === 'eqp' ? $t('basEquipment.equipment') : $t('basEquipment.cutter')}}
            </el-tag>
          </template>
            </el-table-column>


            <el-table-column prop="eqpSpec" :label="$t('basEquipment.equipmentSpecification')"/>
            <el-table-column prop="supplier" :label="$t('basEquipment.supplier')" width="240">
            </el-table-column>
            <el-table-column :label="$t('basEquipment.operation')" width="220">
              <template #default="scope">
                <el-button icon="edit" type="primary" v-auth="'platform_basDicSet_edit'"
                           @click="equiptmentDialogRef.openEditDialog(scope.row)">{{$t('basEquipment.edit')}}
                </el-button>
                <el-button  type="primary" plain icon="View"
                           @click="equiptmentDialogRef.openViewDialog(scope.row)">
                  {{$t('basEquipment.view')}}
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination"/>
        </div>
        <eqpForm ref="equiptmentDialogRef" @refresh="getDataList(false)"/>
    </div>
</template>

<script setup lang="ts" >
import { defineAsyncComponent, reactive, ref } from "vue";
import { BasicTableProps, useTable } from "/@/hooks/table";
import {useMessage, useMessageBox} from "/@/hooks/message";
import {GetEquipmentLedgerList,DeleteEquipmentLedger,getValueSet} from "/@/api/platform/digital/basEquipment";
import {useI18n} from 'vue-i18n';
const {t} = useI18n();
const eqpForm =  defineAsyncComponent(() => import('./eqpForm.vue'));
//下拉框
const options = ref([
  { text: t('basEquipment.all'), value: 'All' }  
])
// const options = ref([
//     { label: t('basEquipment.all'), value: 'All' },
//     { label: t('basEquipment.equipment'), value: 'eqp' },
//     { label: t('basEquipment.cutter'), value: 'cut' }
// ]);
onMounted(() => {
  getValueSet({ code: 'DeviceType' }).then(res => {
    res.data.forEach(element => {
      options.value.push({ text: element.text, value: element.value })
    });
  })
})
// 搜索变量
const queryRef = ref()
const equiptmentDialogRef = ref()

const state: BasicTableProps = reactive<BasicTableProps>({
    queryForm: {},
    pageList: GetEquipmentLedgerList
})

state.queryForm.eqpType='All'

const {
    getDataList,
    currentChangeHandle,
    sizeChangeHandle,
    sortChangeHandle,
    downBlobFile,
    tableStyle
} = useTable(state)

// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)

//双击
const dblclickRow = (row: any) => {
  equiptmentDialogRef.openAddDialog()
}

// 多选事件
const selectionChangeHandle = (objs: { id: string }[]) => {
  selectObjs.value = objs.map(({id}) => id);
  multiple.value = !objs.length;
};
// 删除操作
const handleDelete = async (ids: Array<number>) => {
  if (ids?.length === 0) {
    return useMessage().warning(t('basEquipment.selectDelete'))
  }

  const idArray: IdArray = ids.map(item => (item));

  useMessageBox().confirm(t('basEquipment.permanentlyDeleted')).then(async () => {
    try {
      await DeleteEquipmentLedger({
        Ids : idArray
      })
      getDataList();
      useMessage().success(t('basEquipment.deleteSuccess'));
    } catch (err: any) {
      useMessage().error(t('basEquipment.deleteFaild') + err.msg)
    }
  })
};


// 清空搜索条件
const resetQuery = () => {
  // 清空搜索条件
  queryRef.value?.resetFields()
  // 清空多选
  selectObjs.value = []
  getDataList(false)
}

</script>